namespace WebApi101.Data.Migrations
{
    using WebApi101.Data.Models;
    using Microsoft.AspNet.Identity.EntityFramework;
    using Microsoft.AspNet.Identity;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<WebApi101.Data.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(WebApi101.Data.ApplicationDbContext context)
        {
            /// seddd....
            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(context);
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);

            ApplicationUser julio = userManager.FindByName("julio@codercamps.com");
            ApplicationUser dan = userManager.FindByName("dan@codercamps.com");
            // get user... Julio
            if(julio == null){
                julio = new ApplicationUser() {
                    UserName = "julio@codercamps.com",
                    Email = "julio@codercamps.com"
                };
                userManager.Create(julio, "Julio123!");
                julio = userManager.FindByName("julio@codercamps.com");
            }
            // get user... Dan
            if (dan == null) {
                dan = new ApplicationUser()
                {
                    UserName = "dan@codercamps.com",
                    Email = "dan@codercamps.com"
                };
                userManager.Create(dan, "Julio123!");
                dan = userManager.FindByName("dan@codercamps.com");
            }

            context.Todos.AddOrUpdate(t => t.Text,
                new Todo {
                    City="Htown",
                    Text="Get wrk done",
                    User= julio,
                    CreatedUser=julio,
                    UpdatedUser=julio,
                    DateCreated = DateTime.Now,
                    DateUpdated = DateTime.Now,
                    IsCompleted = false

                },
                new Todo
                {
                    City = "Ptown",
                    Text = "Pollo Tropical",
                    User = dan,
                    CreatedUser = dan,
                    UpdatedUser = dan,
                    DateCreated = DateTime.Now,
                    DateUpdated = DateTime.Now,
                    IsCompleted = false

                }

                );
            context.SaveChanges();

 

            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}
